package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.Status;
import com.gxa.entity.SysAccount;
import com.gxa.entity.SysProviderProgress;
import com.gxa.mapper.AccountManagementMapper;
import com.gxa.service.AccountManagementService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountManagementServiceImpl implements AccountManagementService {

    @Autowired
    AccountManagementMapper accountManagementMapper;


    /**
     * 添加账号
     * @param sysAccount
     * @return
     */
    @Override
    public R addAccount(SysAccount sysAccount) {
        R r = new R();
        int i = accountManagementMapper.addAccount(sysAccount);
        if (i>0){
            r.setCode(200);
            r.setMsg("success");
        }else{
            r.setCode(500);
            r.setMsg("服务器开小差了");
        }
        return r;
    }

    /**
     * 查询所有账号
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R findAccountList(Integer currentPage, Integer pageSize) {
        R r = new R();
        if(currentPage !=null && pageSize !=null){
        PageHelper.startPage(currentPage,pageSize);
        List accountList = accountManagementMapper.findAccountList();
        PageInfo<SysProviderProgress> pageInfo = new PageInfo<>(accountList);
        if(pageInfo != null ){
            r.setCode(200);
            r.setMsg("success");
            r.setData(pageInfo);
        }else{
            r.setCode(500);
            r.setMsg("服务器开小差了");
        }
        }else{
            r.fromStatus(Status.BAD_QUERY_PARAM);
        }
        return r;
    }

    /**
     * 根据开户人查账号
     * @param name
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R findAccountByName(String name, Integer currentPage, Integer pageSize) {
        R r = new R();
        PageHelper.startPage(currentPage,pageSize);
        List accountByName = accountManagementMapper.findAccountByName(name);
        PageInfo<SysProviderProgress> pageInfo = new PageInfo<>(accountByName);
        if(pageInfo != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(pageInfo);
        }else{
            r.setCode(500);
            r.setMsg("服务器开小差了");
        }
        return r;
    }

    /**
     * 更新账号
     * @param sysAccount
     * @return
     */
    @Override
    public R updateAccount(SysAccount sysAccount) {
        R r = new R();
        int i = accountManagementMapper.updateAccount(sysAccount);
        if(i>0){
            r.setCode(200);
            r.setMsg("success");
        }else {
            r.setCode(500);
            r.setMsg("服务器开小差了");
        }
        return r;
    }

    /**
     * 根据id删除账号
     * @param id
     * @return
     */
    @Override
    public R deleteAccount(Integer id) {
        R r = new R();
        int i = accountManagementMapper.deleteAccount(id);
        if(i>0){
            r.setCode(200);
            r.setMsg("success");
        }else {
            r.setCode(500);
            r.setMsg("服务器开小差了");
        }
        return r;
    }
}
